/**   
* @Title: WebUtil.java 
* @Package com.csis.system.common 
* @Description: TODO
* @author dailey  
* @date 2012-10-6
* @version V1.0   
*/
package com.csis.system.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.csis.framework.domain.FormBean;
import com.csis.security.domain.Role;
import com.csis.system.common.convert.Converter;
import com.csis.system.common.convert.Converter.ConvertArray;
import com.csis.system.domain.User;

/**
 * @author dailey
 *
 */
public final class WebUtil {

	/**
	 * get current user from current session
	 * @param session
	 */
	public static User getSessionUser(HttpSession session) {
		User user = (User) session.getAttribute(IStaticData.SESSION_USER);
		if (user == null) {
			throw new IllegalArgumentException("Session Invalid.");
		}
		return user;
	}

	public static void saveSessionUser(HttpSession session, User user) {
		session.setAttribute(IStaticData.SESSION_USER, user);
	}

	public static Converter buildConvert(Object source) {
		return new Converter(source);
	}

	public final static Format DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmsss");

	/**
	 * 20 characters
	 * @return String like 201210072102013
	 */
	public static String generateNo() {
		return DATE_FORMAT.format(new Date());
	}

	public static boolean isContains(String container, String[] regx) {
		boolean result = false;
		for (int i = 0; i < regx.length; i++) {
			if (container.indexOf(regx[i]) != -1) {
				return true;
			}
		}
		return result;
	}

	public static boolean isAdminRole(HttpSession session) {
		User user = getSessionUser(session);
		if (user != null) {
			for (Role role : user.getRoles()) {
				if (IStaticData.ROLE_ADMIN.equals(role.getId()) || role.getId() == IStaticData.ROLE_ADMIN) {
					return true;
				}
			}
		}
		return false;
	}

	public static boolean isManagerRole(HttpSession session) {
		User user = getSessionUser(session);
		if (user != null) {
			for (Role role : user.getRoles()) {
				if (role.getId() == IStaticData.ROLE_MANAGER || IStaticData.ROLE_MANAGER.equals(role.getId())) {
					return true;
				}
			}
		}
		return false;
	}

	public static boolean isShowAll(FormBean<?> formBean) {
		if (formBean.getIsAll() != null && true == formBean.getIsAll()) {
			return true;
		}
		return false;
	}

	public static byte[] getDefaultProductImage(HttpServletRequest request) throws Exception {
		String rootPath = request.getSession().getServletContext().getRealPath("/");
		String defaultPicPath = rootPath + File.separator + IStaticData.RESOURCE_IMG_DEFAULT_JPG;
		File defaultPic = new File(defaultPicPath);
		return getBytesFromImageFile(defaultPic);
	}
	
	public static byte[] getImage(HttpServletRequest request,String imageName) throws Exception {
		String rootPath = request.getSession().getServletContext().getRealPath("/");
		String picPath = rootPath + File.separator + IStaticData.RESOURCE_IMG_PATH+File.separator+imageName;
		File picFile = new File(picPath);
		return getBytesFromImageFile(picFile);
	}

	/**
	 * @param defaultPic
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static byte[] getBytesFromImageFile(File defaultPic) throws FileNotFoundException, IOException {
		if (defaultPic == null)
			return null;
		FileInputStream fin = new FileInputStream(defaultPic);
		byte[] bytes = new byte[fin.available()];
		fin.read(bytes);
		fin.close();
		return bytes;
	}

	public static void main(String[] args) {
		System.out.println(WebUtil.generateNo());
		ConvertArray<Integer> ids = WebUtil.buildConvert("1,2,3".split(",")).convertToArray(int.class);
		for (Integer id : ids) {
			System.out.println(id);
		}
	}

}
